package com.dyrj.ysk.util;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * 表单生成工具
 * @author 吴侠志  2019/10/18
 * @version 1.0
 */
public class FormUtil {
    /**
     * 创建自动提交表单
     * @param reqUrl
     * @param hiddens
     * @param encoding
     * @return
     */
    public static String createAutoFormHtml(String reqUrl, Map<String, String> hiddens, String encoding) {
        StringBuffer sf = new StringBuffer();
        sf.append("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset="+encoding+"\"/></head><body>");
        sf.append("<form id = \"form\" action=\"" + reqUrl
                + "\" method=\"post\">");
        if (null != hiddens && 0 != hiddens.size()) {
            Set<Map.Entry<String, String>> set = hiddens.entrySet();
            Iterator<Map.Entry<String, String>> it = set.iterator();
            while (it.hasNext()) {
                Map.Entry<String, String> ey = it.next();
                String key = ey.getKey();
                String value = ey.getValue();
                sf.append("<input type=\"hidden\" name=\"" + key + "\" id=\""
                        + key + "\" value=\"" + value + "\"/>");
            }
        }
        sf.append("</form>");
        sf.append("</body>");
        sf.append("<script type=\"text/javascript\">");
        sf.append("document.all.form.submit();");
        sf.append("</script>");
        sf.append("</html>");
        return sf.toString();
    }

    /**
     * 创建自动提交表单
     * @param reqUrl
     * @param hiddens
     * @return
     */
    public static String createAutoFormHtml(String reqUrl, Map<String, String> hiddens) {
        return createAutoFormHtml(reqUrl,hiddens,"UTF-8");
    }
}
